package com.qfmy.service.impl.novel;

import com.qfmy.mapper.novel.NovelSelfMapper;
import com.qfmy.service.novel.NovelSelfService;
import com.qfmy.vo.novel.NovelSelfVo;
import common.exception.BaseException;
import common.login.LoginUser;
import common.login.LoginUserHolder;
import common.result.ResultCodeEnum;
import model.entity.novel.NovelSelf;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 清风明月
 * @Package com.qfmy.service.impl.novel
 * @date 2025/6/7
 * @description 书架
 */
@Service
@SuppressWarnings("all")
public  class NovelSelfServiceImpl implements NovelSelfService {

    /**
     * 注入mapper
     */
    @Autowired
    private NovelSelfMapper novelSelfMapper;
    private Long userId;

    /**
     * 获取书架
     * @param pageNum
     * @param size
     * @return
     */
    @Override
    public List<NovelSelfVo> getNovelSelf(Integer pageNum, Integer size) {
        //计算分页
        Integer start = getStart(pageNum, size);
        //获取用户信息
        Long userId = getUserId();
        //根据用户id + 分页获取书架
        List<NovelSelf> novelSelfs = novelSelfMapper.getNovelSelf(userId, start, size);
        //拷贝数据
        List<NovelSelfVo> novelSelfVos = Copy(novelSelfs);
        return novelSelfVos;
    }

    /**
     * 添加小说到书架
     * @param novelId
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addNovelSelf(@NotNull Long novelId) {
        //创建书架对象
        NovelSelf novelSelf = new NovelSelf();
        novelSelf.setNovelId(novelId.intValue());
        novelSelf.setUserId(getUserId().intValue());
        novelSelfMapper.insert(novelSelf);
    }

    /**
     * 删除小说
     * @param novelId
     */
    @Override
    public void deleteNovelSelf(@NotNull Long novelId) {
        //获取用户id
        Long userId = getUserId();
        novelSelfMapper.deleteNovelSelf(novelId.intValue(), userId.intValue());
    }


    /**
     * 拷贝数据
     * @param novelSelfs
     * @return
     */
    @Nullable
    @Contract(pure = true)
    private List<NovelSelfVo> Copy(List<NovelSelf> novelSelfs) {
        //判断集合是否为空
        if (novelSelfs == null || novelSelfs.isEmpty() || novelSelfs.size() == 0)
        {
            return new ArrayList<>();
        }
        //创建集合
        List<NovelSelfVo> novelSelfVos = new ArrayList<>();
        //拷贝数据
        for (NovelSelf novelSelf : novelSelfs) {
            NovelSelfVo novelSelfVo = new NovelSelfVo();
            novelSelfVo.setNovelId(novelSelf.getNovelId());
            novelSelfVo.setChapter(novelSelf.getChapter());
            novelSelfVo.setNovel(novelSelf.getNovel());
            novelSelfVos.add(novelSelfVo);
        }
        return novelSelfVos;
    }

    /**
     *
     * @param pageNum
     * @param size
     * @return
     */
    @Nullable
    @Contract(pure = true)
    private Integer getStart(Integer pageNum, Integer size) {
        if (pageNum == null || size == null)
        {
            size = 20;
            pageNum = 1;
        }
        return (pageNum - 1) * size;
    }

    /**
     * 获取用户ID
     * @return
     */
    public Long getUserId() {
        LoginUser loginUser = LoginUserHolder.getLoginUser();
        //判断是否存在
        if(loginUser == null)
        {
            throw new BaseException(ResultCodeEnum.TOKEN_INVALID);
        }
        //获取用户id
        return loginUser.getUserId();
    }
}
